package cn.iocoder.yudao.module.medical.controller.admin.grafify;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.medical.controller.admin.grafify.vo.*;
import cn.iocoder.yudao.module.medical.dal.dataobject.grafify.GrafifyDO;
import cn.iocoder.yudao.module.medical.service.grafify.GrafifyService;

@Tag(name = "管理后台 - 绘图")
@RestController
@RequestMapping("/medical/grafify")
@Validated
public class GrafifyController {

    @Resource
    private GrafifyService grafifyService;

    @PostMapping("/create")
    @Operation(summary = "创建绘图")
    @PreAuthorize("@ss.hasPermission('medical:grafify:create')")
    public CommonResult<Integer> createGrafify(@Valid @RequestBody GrafifySaveReqVO createReqVO) {
        return success(grafifyService.createGrafify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘图")
    @PreAuthorize("@ss.hasPermission('medical:grafify:update')")
    public CommonResult<Boolean> updateGrafify(@Valid @RequestBody GrafifySaveReqVO updateReqVO) {
        grafifyService.updateGrafify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘图")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('medical:grafify:delete')")
    public CommonResult<Boolean> deleteGrafify(@RequestParam("id") Integer id) {
        grafifyService.deleteGrafify(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘图")
                @PreAuthorize("@ss.hasPermission('medical:grafify:delete')")
    public CommonResult<Boolean> deleteGrafifyList(@RequestParam("ids") List<Integer> ids) {
        grafifyService.deleteGrafifyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘图")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('medical:grafify:query')")
    public CommonResult<GrafifyRespVO> getGrafify(@RequestParam("id") Integer id) {
        GrafifyDO grafify = grafifyService.getGrafify(id);
        return success(BeanUtils.toBean(grafify, GrafifyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘图分页")
    @PreAuthorize("@ss.hasPermission('medical:grafify:query')")
    public CommonResult<PageResult<GrafifyRespVO>> getGrafifyPage(@Valid GrafifyPageReqVO pageReqVO) {
        PageResult<GrafifyDO> pageResult = grafifyService.getGrafifyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GrafifyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘图 Excel")
    @PreAuthorize("@ss.hasPermission('medical:grafify:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGrafifyExcel(@Valid GrafifyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GrafifyDO> list = grafifyService.getGrafifyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘图.xls", "数据", GrafifyRespVO.class,
                        BeanUtils.toBean(list, GrafifyRespVO.class));
    }

}